home *** CD-ROM | disk | FTP | other *** search
- Path: casbah.acns.nwu.edu!muzaffar
- From: muzaffar@casbah.acns.nwu.edu (Usman Muzaffar)
- Newsgroups: comp.lang.c
- Subject: Re: problem passing a string
- Date: 9 Mar 1996 12:55:36 GMT
- Organization: Northwestern University, Evanston IL
- Message-ID: <4hrv48$huo@news.acns.nwu.edu>
- References: <4hrtsp$4mf@mulgave.octacon.co.uk>
- NNTP-Posting-Host: casbah.acns.nwu.edu
-
- In article <4hrtsp$4mf@mulgave.octacon.co.uk>,
- Nik Palmer <Nik.Palmer@onyx.octacon.co.uk> wrote:
- >Hi, Im using a textoutput routine in the fastgraph package of the
- >form below
- > void fg_print(char *string,int n);
- >The string that I want to pass is initialised as below
- >
- >char string[6]
- >double variable= 1.2345
- >sprintf(string1,"%5f",variable)
- >
- >fg_print(string1,5)
- >
- >problem is it's outputting garbage, I know that string1 is only a
- >pointer to the memory that holds the string, and that if I want to
- >access the string I need string1[num]. So I think it's just
- >outputting 5 bytes of memory from the memory location string1.
- >
- >How do I pass this routine the string??
- >Thanks for your help.
- >Nik
-
- I'll bet anything that "char string[6] " is declared locally
- inside some function. The problem is that the pointer you pass
- refers to memory that fg_print can't access. It's a valid string
- pointer, alright, but not in a valid location.
-
- Remember, only globals & data on the heap (stuff made by malloc and calloc, for
- example) is truly accessible all the time. Anything declared inside
- a function (including main()) is NOT valid outside that function.
-
- Your solution is to make sure "char string[6]" is declared externally
- to all procedures. That is, put it at the top of your program.
-
- -usman
-
-
-
-
-
-